package l1j.server.server.model.damage.physical;

import l1j.server.server.ActionCodes;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.damage.L1DamageOnNpc;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1PcDamage;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.utils.Random;

public class L1PhysicalDamage_Pc_Npc extends L1Dmg {

	private L1PcInstance _attacker;

	private L1NpcInstance _target;

	private L1PcDamage _pcDamage;

	public L1PhysicalDamage_Pc_Npc(L1PcInstance attacker, L1NpcInstance target) {
		_attacker = attacker;
		_target = target;
		_pcDamage = new L1PcDamage(this, attacker, target);
	}

	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}

	@Override
	public void calcDmg() {
		_pcDamage.calcWeaponDmg(_target.getNpcTemplate().get_size()
				.equalsIgnoreCase("small"));
		calcMaterialBlessDmg();
		_pcDamage.calcPcDmg();
		L1DamageOnNpc dop = new L1DamageOnNpc(this, _target);
		dop.calcDamageOnNpc();
		_pcDamage.hitRegenerationHp();
		_pcDamage.effectManaDrain();
		_pcDamage.effectPoision();
		_pcDamage.EffectWeaponMagic();
	}

	// ●●●● 武器の材質と祝福による追加ダメージ算出 ●●●●
	private void calcMaterialBlessDmg() {
		int _weaponMaterial = 0;
		L1ItemInstance weapon = _attacker.getWeapon();
		if (weapon != null) {
			_weaponMaterial = weapon.getItem().getMaterial();
		}
		int undead = _target.getNpcTemplate().get_undead();
		if (((_weaponMaterial == 14) || (_weaponMaterial == 17) || (_weaponMaterial == 22))
				&& ((undead == 1) || (undead == 3) || (undead == 5))) { // 銀・ミスリル・オリハルコン、かつ、アンデッド系・アンデッド系ボス・銀特効モンスター
			this.add(Random.nextInt(20) + 1);
		} else if (((_weaponMaterial == 17) || (_weaponMaterial == 22))
				&& (undead == 2)) { // ミスリル・オリハルコン、かつ、悪魔系
			this.add(Random.nextInt(3) + 1);
		}
		if(weapon != null){
			if (( weapon.getItem().getBless() == 0)
				&& ((undead == 1) || (undead == 2) || (undead == 3))) { // 祝福武器、かつ、アンデッド系・悪魔系・アンデッド系ボス
			this.add(Random.nextInt(4) + 1);
			}
			if ((!weapon.isBow()) && (!weapon.isGauntlet())
					&& (weapon.getHolyDmgByMagic() != 0)
					&& ((undead == 1) || (undead == 3))) {
				this.add(weapon.getHolyDmgByMagic());
			}
		}
		
		
	}
}
